package org.vnl.platform.web.impl.myfaces.dojo.component.renderer;

import java.io.IOException;

import java.util.Map;

//import javax.el.ValueExpression;
import javax.faces.component.UIComponent;
import javax.faces.component.UIOutput;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.faces.event.ActionEvent;
import javax.faces.render.Renderer;

import org.apache.commons.lang3.StringUtils;
import org.vnl.platform.web.impl.myfaces.dojo.component.DojoButton;


//import com.von.platform.impl.myfaces.dojo.component.DojoButton;

public class DojoButtonRenderer extends Renderer {

    private static final String SUFFIX = "_BTN";
    
    @Override
    public void decode(FacesContext context, UIComponent component) {
        if (!(component instanceof DojoButton)) {
            return;
        }
        DojoButton btn = (DojoButton) component;
        String clientId = btn.getClientId();
        Map<String,String> params = context.getExternalContext().getRequestParameterMap();
        for (String key : params.keySet()) {
            System.out.println("key="+key+"\t="+params.get(key)); 
        }
        String value = params.get(clientId+SUFFIX); 
        if (!StringUtils.isBlank(value)) {
            if (value.equals("1"))
                component.queueEvent(new ActionEvent(component));
        }
    }

    @Override
    public void encodeBegin(FacesContext context, UIComponent component) throws IOException {
        ResponseWriter rw = context.getResponseWriter();
        String clientId = component.getClientId();
        rw.startElement("button", component);
        rw.writeAttribute("id", clientId, null);
        rw.writeAttribute("data-dojo-type", "dijit.form.Button", null);
        rw.writeAttribute("type", "submit", null);
        if (component instanceof DojoButton) {
            DojoButton btn = (DojoButton) component; 
            if (!StringUtils.isBlank(btn.getDojoId())) {
                rw.writeAttribute("data-dojo-id", btn.getDojoId(), null);
            }
        }
        Object obj = component.getAttributes().get("value");
        if (obj != null) {
            if (!StringUtils.isBlank(obj.toString())) {
                rw.write(obj.toString());
            }
        } 
        StringBuffer sb = new StringBuffer("dojo.byId('");
        sb.append(clientId.replaceAll(":", "\\\\:"));
        sb.append(SUFFIX);
        sb.append("').value = '1';");
        UIComponent facet = component.getFacet("onClick");
        if (facet != null) {
            if (facet instanceof UIOutput) {
                sb.append(((UIOutput) facet).getValue());
            }
        }
//        rw.writeAttribute("onClick", sb.toString(), null);
        rw.startElement("script", component);
        rw.writeAttribute("type", "dojo/method", null);
        rw.writeAttribute("data-dojo-event", "onClick", null);
        rw.write(sb.toString());
        rw.endElement("script");
    }

    @Override
    public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
        ResponseWriter rw = context.getResponseWriter();
        String clientId = component.getClientId();
        rw.endElement("button");
        rw.startElement("input", component);
        rw.writeAttribute("type", "hidden", null);
        rw.writeAttribute("id", clientId+SUFFIX, null);
        rw.writeAttribute("name", clientId+SUFFIX, null);
        rw.writeAttribute("value", "0", null);
        rw.endElement("input");
    }

}
